//
//  ZqmSeacherView.swift
//  weibo
//
//  Created by 张清美 on 16/3/2.
//  Copyright © 2016年 张清美. All rights reserved.
//

import UIKit

class ZqmSeacherView: UIView {

    @IBOutlet weak var textFieldRightconstraint: NSLayoutConstraint!
    
    @IBOutlet weak var cancelBtn: UIButton!
    
    
    @IBOutlet weak var textField: UITextField!
    
    
  
    //定义类方法
    
    class func seacherView()->ZqmSeacherView {
        
        
        
        return NSBundle.mainBundle().loadNibNamed("SeacherView", owner: nil, options: nil).last as! ZqmSeacherView //强转为控制器类型
        
        
        
    }
    
    
   
   
    //文本框开始改变的点击事件
    
    @IBAction func textFieldDidBegin(sender: UITextField) {
        
        
        //修改后面的约束
        self.textFieldRightconstraint.constant = self.cancelBtn.frame.width
        
        UIView.animateWithDuration(0.5) { () -> Void in
        
        
        //重新布局
        self.textField.layoutIfNeeded()
        
        
        }
        
        
     
        
        
        
        
        
    }
    
    //取消按钮的点击事件
    @IBAction func btnDidClick(sender: UIButton) {
        
       //调整右边的约束
        textFieldRightconstraint.constant = 0
        
        
        //取消第一响应者
        textField.resignFirstResponder()
        
        
        UIView.animateWithDuration(0.5) { () -> Void in
            
            
            //重新布局
            self.textField.layoutIfNeeded()
            
            
        }
        
        
        
    }
    
    //设置 textfield的 leftView 中的放大镜
    
    //
    override func awakeFromNib() {
        
        
        
        leftView.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        
        textField.leftView = leftView
        
        
        textField.leftViewMode = .Always//让放大镜出来
        //设置边界
        textField.layer.borderWidth = 2
        
        textField.layer.borderColor = UIColor.redColor().CGColor
        
        textField.layer.cornerRadius = 10
        
        textField.layer.masksToBounds = true
        
        
        
        
        
    }
    
    
  //懒加载控件
    private lazy var leftView:UIImageView = {
    
    
        let view = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        view.contentMode = .Center//解决放大镜的大小问题 太靠左
        
        
    
        return view
    
    
    }()
    
    
    
 

}
